﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace BaseControls
{
    /// <summary>
    /// This class is base for any interactive items on the screen.
    /// </summary>
    [TemplateVisualState(GroupName = "CustomStates", Name = "ItemComplete")]
    [TemplateVisualState(GroupName = "CustomStates", Name = "Normal")]    
    public class BaseItem : Control
    {
        public BaseItem():base()
        {
            VisualStateManager.GoToState(this, "Normal", true);
            this.DefaultStyleKey = typeof(BaseItem);
        }
        /// <summary>
        /// Resources that will be download and load before scene is show.
        /// </summary>
        public static readonly DependencyProperty ResourcesNeededProperty = DependencyProperty.Register("ResourcesNeeded", typeof(string), typeof(BaseItem), null);        
        public static readonly DependencyProperty ScenarioNameProperty = DependencyProperty.Register("ScenarioName", typeof(string), typeof(BaseItem), new PropertyMetadata("Unknown object"));
        public static readonly DependencyProperty ActiveProperty = DependencyProperty.Register("Active", typeof(bool), typeof(BaseItem), new PropertyMetadata(true));
        public override void OnApplyTemplate()
        {
            Grid layout = this.GetTemplateChild("LayoutRoot") as Grid;
            ImageBackground = layout.FindName("Image") as Image; 
            base.OnApplyTemplate();             
        }        
        /// <summary>
        /// This event is raised when user complete sub task (in hog game this sub task is finded one item of items list).
        /// </summary>
        public event EventHandler ItemComplete;

        protected void OnItemComplete()
        {
            VisualStateManager.GoToState(this, "ItemComplete", true);
            if (ItemComplete != null)
                ItemComplete(this,new EventArgs());
        }
        public string ResourcesNeeded
        {
            get
            {
                return (string)this.GetValue(ResourcesNeededProperty);
            }
            set
            {
                base.SetValue(ResourcesNeededProperty, value);
            }
        }        
        public string ScenarioName
        {
            get
            {
                return (string)this.GetValue(ScenarioNameProperty);
            }
            set
            {
                base.SetValue(ScenarioNameProperty, value);
            }
        }
        public bool Active
        {
            get
            {
                return (bool)this.GetValue(ActiveProperty);
            }
            set
            {
                base.SetValue(ActiveProperty, value);
            }
        }
        private Image ImageBackground;
        public static DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(string), typeof(HogBaseItem), null);
        public string Image
        {
            get
            {
                return (string)this.GetValue(HogBaseItem.ImageProperty);
            }
            set
            {
                this.SetValue(HogBaseItem.ImageProperty, value);
            }
        }
        protected void SetStream(Stream stream)
        {            
            BitmapImage image = new BitmapImage();
            image.SetSource(stream);
            if (double.IsNaN(this.Width))
                this.Width = image.PixelWidth;
            if (double.IsNaN(this.Height))
                this.Height = image.PixelHeight;            
            this.ImageBackground.Source = image;
        }
        /// <summary>
        /// This method is executed from resources manager download method.
        /// </summary>
        /// <param name="stream">Stream that will be pass from download method</param>
        public virtual void AssignStream(Stream stream)
        {
            SetStream(stream);
        }
    }
}
